package com.meida.module.bus.provider.utils;

import com.alibaba.fastjson.util.TypeUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.module.bus.client.entity.BusMaterialPackage100;
import com.meida.module.bus.client.entity.BusMaterialPackage200;
import com.meida.module.bus.client.entity.BusMaterialPlan100;
import com.meida.module.bus.client.entity.BusMaterialPlan200;
import com.meida.module.bus.provider.api.util.ApiConstants;
import com.meida.module.bus.provider.service.BusMaterialPackage100Service;
import com.meida.module.bus.provider.service.BusMaterialPackage200Service;
import com.meida.module.bus.provider.service.BusMaterialPlan100Service;
import com.meida.module.bus.provider.service.BusMaterialPlan200Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Component
public class CommonUtil {
    @Autowired
    private BusMaterialPlan100Service plan100Service;
    @Autowired
    private BusMaterialPlan200Service plan200Service;
    @Autowired
    private BusMaterialPackage100Service package100Service;
    @Autowired
    private BusMaterialPackage200Service package200Service;

    public static void setSelectedMap(EntityMap map) {
        map.put("dataType", "期初数据");
        if (null != map.get("parentId")) {
            if (null != map.get("instructNumber")) {
                map.put("dataType", "修正变更数据");
            } else {
                map.put("dataType", "追加变更数据");
            }
        }
    }

    /**
     *
     * @param projectId
     * @param dataKind
     * @param ExcelList
     * @return True 已存在 False 未存在
     */
    public Boolean ckLevelNo(Long projectId, String dataKind, List<Map<Object, Object>> ExcelList) {
        List<String> levelNos = Lists.newArrayList();
        ExcelList.stream().forEach(excelObj->levelNos.add(TypeUtils.castToString(excelObj.get("层级号"))));

        Boolean rs = Boolean.FALSE;
        Map<String, Object> searchMap = Maps.newHashMap();
        searchMap.put(ApiConstants.MAP_PROJECTID, projectId);

        if(ApiConstants.WZ_PLANDATA_100.equals(dataKind)) {
            Collection<BusMaterialPlan100> plan100s = plan100Service.listByMap(searchMap);
            if(0 < plan100s.size()) {
               rs = plan100s.stream().anyMatch(planOjb->levelNos.contains(planOjb.getLevelNo()));
            }
        } else if(ApiConstants.WZ_PLANDATA_200.equals(dataKind)) {
            Collection<BusMaterialPlan200> plan200s = plan200Service.listByMap(searchMap);
            if(0 < plan200s.size()) {
                rs = plan200s.stream().anyMatch(planOjb->levelNos.contains(planOjb.getLevelNo()));
            }
        } else if(ApiConstants.FB_PLANDATA_100.equals(dataKind)) {
            Collection<BusMaterialPackage100> package100s = package100Service.listByMap(searchMap);
            if(0 < package100s.size()) {
                rs = package100s.stream().anyMatch(planOjb->levelNos.contains(planOjb.getLevelNo()));
            }
        } else if(ApiConstants.FB_PLANDATA_200.equals(dataKind)) {
            Collection<BusMaterialPackage200> package200s = package200Service.listByMap(searchMap);
            if(0 < package200s.size()) {
                rs = package200s.stream().anyMatch(planOjb->levelNos.contains(planOjb.getLevelNo()));
            }
        }
        rs = Boolean.FALSE;
        return rs;
    }

}